package com.piece.admin.controller.oauth;

import com.piece.admin.model.OauthClientDetails;
import com.piece.admin.service.OauthClientDetailsService;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.constant.FrameWorkConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@Api(tags = {"应用授权管理"})
@RequestMapping("/api/oauth")
public class ClientDetailsController extends FrameWorkController {

    @Resource
    private OauthClientDetailsService oauthClientDetailsService;

    @PostMapping("/list")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "查询应用授权列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = oauthClientDetailsService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "查询应用授权分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = oauthClientDetailsService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "加载应用授权表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        String id = Convert.toStr(getRequest().getParameter("id"), null);
        OauthClientDetails oauthClientDetails = new OauthClientDetails();
        if (StringUtil.isNotEmpty(id)) {
            oauthClientDetails = oauthClientDetailsService.findById(id);
        }
        return success(oauthClientDetails);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "system:oauth:add,system:oauth:edit")
    @ApiOperation(value = "保存应用授权表单数据")
    @ApiImplicitParam(value = "应用授权数据", dataTypeClass = OauthClientDetails.class, type = "body")
    public AjaxResponse save() {
        OauthClientDetails oauthClientDetails;
        try {
            boolean insert = false;
            String clientId = Convert.toStr(getRequest().getParameter("clientId"));
            oauthClientDetails = oauthClientDetailsService.findById(clientId);
            if (null == oauthClientDetails) {
                insert = true;
                oauthClientDetails = new OauthClientDetails();
            }
            initDB(oauthClientDetails);
            oauthClientDetailsService.insertOrUpdate(oauthClientDetails, insert);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "system:oauth:delete")
    @ApiOperation(value = "删除应用授权数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        oauthClientDetailsService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "system:oauth:delete")
    @ApiOperation(value = "删除应用授权数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            Arrays.stream(ids.split(FrameWorkConstants.SPLIT_PARAM)).forEach(clientId -> {
                oauthClientDetailsService.delete(clientId);
            });
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
